package com.code.generator.cn.service.impl;

import com.code.generator.cn.entity.CoderTable;
import com.code.generator.cn.mapper.CoderTableMapper;
import com.code.generator.cn.service.ICoderTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CoderTableServiceImpl implements ICoderTableService {

    @Autowired
    private CoderTableMapper coderTableMapper;


    @Override
    public List<CoderTable> getListByCatalog(String data) {
        return coderTableMapper.getListByCatalog(data);
    }

    @Override
    public void saveList(List<Map<String, Object>> maps) {
        coderTableMapper.saveList(maps);
    }

    @Override
    public void deleteList(List<Map<String, Object>> delTableList) {
        coderTableMapper.deleteList(delTableList);
    }

    @Override
    public CoderTable getById(String id) {
        return coderTableMapper.selectByPrimaryKey(Integer.parseInt(id));
    }

    @Override
    public void update(CoderTable coderTable) {
        coderTableMapper.updateByPrimaryKeySelective(coderTable);
    }

    @Override
    @Transactional
    public List<CoderTable> getTables(List<Map<String, Object>> maps, String data) {

        List<CoderTable> tables = getListByCatalog(data);
        if (tables.size() == 0) {
            saveList(maps);
        } else {
            //同步数据库和反射出来的数据，追加
            List<Map<String, Object>> tabNameList = getCreateMaps(maps, tables,data);
            if (tabNameList.size() > 0) {
                saveList(tabNameList);
            }
            tables = getListByCatalog(data);
            List<Map<String, Object>> delTableList = getDeleteList(maps, tables,data);
            if (delTableList.size() > 0) {
                deleteList(delTableList);
            }
        }
        return  getListByCatalog(data);
    }

    /**
     * 获取需要删除的数据
     *
     * @param maps
     * @param tables
     * @return
     */
    private List<Map<String, Object>> getDeleteList(List<Map<String, Object>> maps, List<CoderTable> tables,String data) {
        List<Map<String, Object>> delTableList = new ArrayList<>();
        a:
        for (CoderTable table : tables) {
            for (int i = 0; i < maps.size(); i++) {
                boolean flag=true;
                String tableName = maps.get(i).get("name").toString();
                if (tableName.equals(table.getTableName())) {
                    continue a;
                }
                if (i == maps.size() -1) {
                    if (!tableName.equals(table.getTableName())) {
                        Map<String, Object> dataMap = new HashMap<>();
                        dataMap.put("data", data);
                        dataMap.put("name", table.getTableName());
                        delTableList.add(dataMap);
                    }
                }
            }
        }
        return delTableList;
    }


    /**
     * 获取需要新增的数据库表
     *
     * @param maps
     * @param tables
     * @return
     */
    private List<Map<String, Object>> getCreateMaps(List<Map<String, Object>> maps, List<CoderTable> tables,String data) {
        List<Map<String, Object>> tabNameList = new ArrayList<>();
        a:
        for (Map<String, Object> map : maps) {
            String tableName = map.get("name").toString();
            String modifyName= map.get("modifyName").toString();
            for (int i = 0; i < tables.size(); i++) {
                String dataTableName = tables.get(i).getTableName();
                if (dataTableName.equals(tableName)) {
                    continue a;
                }
                if (i == tables.size() - 1) {
                    if (!dataTableName.equals(tableName)) {
                        Map<String, Object> dataMap = new HashMap<>();
                        dataMap.put("data", data);
                        dataMap.put("name", tableName);
                        dataMap.put("modifyName",modifyName);
                        tabNameList.add(dataMap);
                    }
                }
            }
        }
        return tabNameList;
    }
}
